﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Mersiv.Lib.Wrapper;
using Mersiv.Lib.Wrapper.Command;
using Mersiv.Lib.Wrapper.Event;

namespace Mersiv.App.Client.Data
{

  /// <summary>
  /// NOTE: This class should be split into DataController and DataModel classes.
  /// </summary>
  internal class DataModel
  {

    public string FileName { get; set; }
    public List<HgRepository> RepositoryList { get; set; }

    public DataModel()
    {
      this.RepositoryList = new List<HgRepository>();
    }

    #region Add

    public void Add(HgRepository hgRepository)
    {
      this.AddInternal(hgRepository);
      this.Save();    
    }

    /// <summary>
    /// Add without saving the list. Called from the Load() function.
    /// </summary>
    /// <param name="hgRepository"></param>
    private void AddInternal(HgRepository hgRepository)
    {
      this.RepositoryList.Add(hgRepository);
      this.OnRepositoryAdded(hgRepository); 
    }

    #endregion

    #region Remove

    public void Remove(HgRepository hgRepository)
    {
      this.RepositoryList.Remove(hgRepository);
      this.OnRepositoryRemoved(hgRepository);
      this.Save();
    }

    #endregion

    public bool NotContains(HgRepository hgRepository)
    {
      return !this.RepositoryList.Contains(hgRepository);
    }

    //
    // IO
    //

    private static readonly string DELIMITER = "|||";


    public void Save()
    {
      StringBuilder stringBuilder = new StringBuilder();
      foreach (HgRepository hgRepository in this.RepositoryList)
      {
        stringBuilder.Append(hgRepository.Name);
        stringBuilder.Append(DataModel.DELIMITER);
        stringBuilder.Append(hgRepository.WorkingDirectory);
        stringBuilder.Append(Environment.NewLine);
      }
      using (StreamWriter streamWriter = new StreamWriter(this.FileName))
      {
        streamWriter.Write(stringBuilder.ToString());
      }
    }

    public void Load()
    {
      if (File.Exists(this.FileName))
      {
        this.RepositoryList.Clear();
        using (StreamReader sr = new StreamReader(this.FileName))
        {
          string fullFileContents = sr.ReadToEnd();
          if (fullFileContents != null && fullFileContents != string.Empty)
          {
            string[] fileLines = fullFileContents.Split(Environment.NewLine.ToCharArray());
            foreach (string line in fileLines)
            {
              if (line != null && line != string.Empty)
              {
                string[] lineParts = line.Split(new string[] { DataModel.DELIMITER }, StringSplitOptions.None);
                if (lineParts.Length > 1)
                {
                  HgRepository hgRepository = new HgRepository(lineParts[0], lineParts[1]);
                  this.AddInternal(hgRepository);
                }
                else
                {
                  // TODO: Logging?
                }
              }
            }
            this.RepositoryList.Sort();
          }
        }
      }
      else
      {
        //TODO: nothing???
      }
    }


    //
    // Events
    //

    public event HgRepositoryEvent RepositoryAdded;
    protected virtual void OnRepositoryAdded(HgRepository hgRepository)
    {
      if (this.RepositoryAdded != null)
      {
        this.RepositoryAdded(hgRepository);
      }
    }

    public event HgRepositoryEvent RepositoryRemoved;
    protected virtual void OnRepositoryRemoved(HgRepository hgRepository)
    {
      if (this.RepositoryRemoved != null)
      {
        this.RepositoryRemoved(hgRepository);
      }
    }


  }
}